/**
 * 
 */
package com.flying.dcss.dcmanager.action;

import java.util.Date;
import java.util.UUID;

import javax.persistence.Id;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.dcmanager.bo.impl.DcssDCInfo;
import com.flying.dcss.dcmanager.service.impl.DCInfoService;
import com.flying.dcss.dcmanager.service.impl.DCTreeService;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class CreateDCAction.
 * 
 * @author EvanHong
 */
public class CreateDCAction extends ActionSupport {

	/** The dc info service. */
	private DCInfoService dcInfoService;

	/** The user service. */
	private UserService userService;

	/**
	 * Sets the dc info service.
	 * 
	 * @param dcInfoService
	 *            the new dc info service
	 */
	public void setdcInfoService(DCInfoService dcInfoService) {
		this.dcInfoService = dcInfoService;
	}

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		// Get ajax data.
		String desciption = ServletActionContext.getRequest().getParameter(
				"desciption").toString();
		String fatherId = ServletActionContext.getRequest().getParameter(
				"fatherId").toString();
		String ownerEmail = ServletActionContext.getRequest().getParameter(
				"ownerEmail").toString();
		String currentUserId = ServletActionContext.getRequest().getParameter(
				"currentUserId").toString();

		// If manager isn't exist.
		if (userService.getUserByEmail(ownerEmail) == null)
			return this.ERROR;

		String managerId = userService.getUserByEmail(ownerEmail).getId();

		// Get current user Id.
		DcssUser user = userService.getUserById(currentUserId);
		if (user == null)
			return this.ERROR;

		DcssDCInfo dcInfo = new DcssDCInfo(UUID.randomUUID().toString(),
				desciption, managerId, fatherId, user.getId(), 0, new Date(), 0);

		dcInfoService.addDcInfo(dcInfo);
		return this.SUCCESS;
	}
}
